package util;

import java.io.*;
import java.net.*;

public class UrlFileUtil {

	public static boolean saveUrlAs(String photoUrl, String path) {
		DataInputStream in = null;
		DataOutputStream out = null;
		boolean bool = true;

		try {
			URL url = new URL(photoUrl);
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			connection
					.setRequestProperty("User-Agent",
							"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1) Gecko/Firefox/2.0");
			in = new DataInputStream(connection.getInputStream());
			out = new DataOutputStream(new FileOutputStream(path));
			byte buffer[] = new byte[4096];
			for (int count = 0; (count = in.read(buffer)) > 0;) {
				out.write(buffer, 0, count);
			}

		} catch (IOException e) {
			e.printStackTrace();
			(new File(path)).delete();
			bool = false;
		}finally{	
			try {
				if (out != null)  out.close();
				if (in != null)  in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	
		return bool;
	}

	public static boolean saveUrlAs(String photoUrl, String path,
			String fileType) {
		DataInputStream in = null;
		DataOutputStream out = null;
		boolean bool = true;
		
		String mine = "";
		HttpURLConnection connection = null;
		URL url = null;
		try {
			url = new URL(photoUrl);
			connection = (HttpURLConnection) url.openConnection();
			
			connection.setRequestProperty("User-Agent",
			"Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.1)");
			mine = connection.getContentType().toLowerCase();
			System.out.println((new StringBuilder("MIME\u7C7B\u578B:")).append(mine).toString());
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			in = new DataInputStream(connection.getInputStream());
			out = new DataOutputStream(new FileOutputStream(path));
			byte buffer[] = new byte[4096];
			for (int count = 0; (count = in.read(buffer)) > 0;) {
				out.write(buffer, 0, count);
			}

		} catch (IOException e) {
			System.out
					.println("\u4FDD\u5B58\u6587\u4EF6\u5931\u8D25\u3002\u3002\u3002\u3002\u3002\u3002\u3002");
			e.printStackTrace();
			(new File(path)).delete();
			bool = false;
		}finally{
			try {
				if (out != null)
					out.close();
				if (in != null)
					in.close();
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
				
		}
	
		return bool;
	}

	public static DataInputStream createUrlStream(String photoUrl) {
		DataInputStream dis = null;
		URL url = null;
		try {
			url = new URL(photoUrl);
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			connection.setRequestProperty("User-Agent",
					"Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.1)");
			dis = new DataInputStream(connection.getInputStream());
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if (dis != null)
					dis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return dis;
	}

	public static boolean saveToFile(DataInputStream in, String path) {
		boolean bool = true;
		DataOutputStream out = null;
		try {
			out = new DataOutputStream(new FileOutputStream(path));
			byte buffer[] = new byte[4096];
			for (int count = 0; (count = in.read(buffer)) > 0;) {
				out.write(buffer, 0, count);
			}

		} catch (IOException ex) {
			(new File(path)).delete();
			bool = false;
		}finally{
			try {
				if (out != null)
					out.close();
				if (in != null)
					in.close();
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
		}
		
		return bool;
	}

	public static String readText(File file) throws Exception {
		StringBuffer sb = new StringBuffer("");
		BufferedReader in = null;

		try {
			FileReader fr = new FileReader(file);
			in = new BufferedReader(fr);
			for (String line = new String(); (line = in.readLine()) != null;) {
				sb.append(line);
			}

		} catch (Exception e) {
			sb = new StringBuffer("-1");
			throw e;
		}finally{
			try {
				if (in != null)
					in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return sb.toString();
	}
}
